// pages/main/main.js
const XLSX=require('../../utils/xlsx.mini.min')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        group:"",
        shoulddarrive:"",
        actuallyarrive:"",
        exchangestudent:"",
        informant:"",
        stayname:"",
        leavename:"",
        abname:"",
    },
    formSubmit:function(e){
        // console.log(e.detail.value);
        var group=e.detail.value.group
        var shoulddarrive=e.detail.value.shoulddarrive
        var actuallyarrive=e.detail.value.actuallyarrive
        var exchangestudent=e.detail.value.exchangestudent 
        var informant=e.detail.value.informant 
        var stayname=e.detail.value.stayname 
        var leavename=e.detail.value.leavename 
        var abname=e.detail.value.abname 
        // 具体操作
        if (group==''||shoulddarrive==''||actuallyarrive==''||exchangestudent==''||informant=='') {
            wx.showToast({
              title: '请输入非空字符',
              icon:'error'
            })
        }else{
            this.setData({
                group:group,
                shoulddarrive:shoulddarrive,
                actuallyarrive:actuallyarrive,
                exchangestudent:exchangestudent,
                informant:informant,
                stayname:stayname,
                leavename:leavename,
                abname:abname,
            })
            console.log('数据提交成功');
        }
    },
    formReset:function(){
        this.setData({
            group:"",
            shoulddarrive:"",
            actuallyarrive:"",
            exchangestudent:"",
            informant:"",
            stayname:"",
            leavename:"",
            abname:"",
        })
    },
    exportExcel:function(){
        // 数据
        let data=[
        {
            group:this.data.group,
            shoulddarrive:this.data.shoulddarrive,
            actuallyarrive:this.data.actuallyarrive,
            exchangestudent:this.data.exchangestudent,
            stayname:this.data.stayname,
            leavename:this.data.leavename,
            abname:this.data.abname,
            informant:this.data.informant,
        }]
        let sheet = []
        // 表头
        let title = ['班级', '班级总人数','实际在校人数','交换生人数','留校名单','请假离校名单','缺勤未打卡名单','填表人']
        sheet.push(title)
        // 数据
        data.forEach(item => {
        let rowcontent = []
        rowcontent.push(item.group)
        rowcontent.push(item.shoulddarrive)
        rowcontent.push(item.actuallyarrive)
        rowcontent.push(item.exchangestudent)
        rowcontent.push(item.stayname)
        rowcontent.push(item.leavename)
        rowcontent.push(item.abname)
        rowcontent.push(item.informant)
        sheet.push(rowcontent)
        })
        // XLSX插件使用
        var ws = XLSX.utils.aoa_to_sheet(sheet);
        var wb = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(wb, ws, "导出信息");
        var fileData = XLSX.write(wb, {
        bookType: "xlsx",
        type: 'base64'
        });

        // 保存的本地地址
        console.log(wx.env.USER_DATA_PATH)
        let filePath = `${wx.env.USER_DATA_PATH}/导出信息.xlsx`
        // 写文件
        const fs = wx.getFileSystemManager()
        fs.writeFile({
        filePath: filePath,
        data: fileData,
        encoding: 'base64',
        success(res) {
            console.log(res)
            const sysInfo = wx.getSystemInfoSync()
            // 导出
            if (sysInfo.platform.toLowerCase().indexOf('windows') >= 0) {
            // 电脑PC端导出
            wx.saveFileToDisk({
                filePath: filePath,
                success(res) {
                console.log(res)
                },
                fail(res) {
                console.error(res)
                util.tips("导出失败")
                }
            })
            } else {
            // 手机端导出
            // 打开文档
            wx.openDocument({
                filePath: filePath,
                success: function (res) {
                console.log('打开文档成功')
                },
                fail: console.error
            })
            }
        },
        fail(res) {
            console.error(res)
            if (res.errMsg.indexOf('locked')) {
            wx.showModal({
                title: '提示',
                content: '文档已打开，请先关闭',
            })
            }
        }
    })
    },
    back:function(){
        wx.navigateBack({
            delta:1
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})